package cn.wolfcode.luowowo.website.web.advice;

import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.utils.AjaxResult;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
public class LoginExceptionHadler {

    //二选一
    @ExceptionHandler(LogicException.class)
    public void HandlerLogicException(Exception e,HttpServletResponse resp) throws IOException {
        e.printStackTrace();
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(JSON.toJSONString(new AjaxResult(false,e.getMessage())));
    }
    /*@ExceptionHandler(LogicException.class)
    @ResponseBody
    public Object HandlerLogicException(Exception e){
        e.printStackTrace();
        return new AjaxResult(false,e.getMessage());
    }*/

    @ExceptionHandler(RuntimeException.class)
    public void HandlerRuntimeException(Exception e,HttpServletResponse resp) throws IOException {
        e.printStackTrace();
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(JSON.toJSONString(new AjaxResult(false,"系统繁忙,请稍后重试")));
    }
}
